package com.norns.torque.dialog

import android.app.Dialog
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import com.gyf.immersionbar.ImmersionBar
import com.norns.torque.utils.dp


/**
 * author : daiwenbo
 * description   : 通用dialog
 */

open class CommonDialog : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = object : Dialog(requireContext(), getTheme()) {
            override fun dismiss() {
                hideKeyboard()
                super.dismiss()
            }
        }
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
//        dialog.window?.let { window ->
//            window.decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
//            window.setFlags(
//                WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN
//            )
//            window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
//        }

        // 设置对话框显示的监听器
//        dialog.setOnShowListener {
//            hideNavigationBar(dialog)
//        }
        return dialog
    }

   private fun hideNavigationBar(dialog:Dialog) {
        dialog.window?.let { window->
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            window.decorView.setOnSystemUiVisibilityChangeListener {
                var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or  //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or  //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN or  //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                uiOptions = uiOptions or 0x00001000
                window.decorView.systemUiVisibility = uiOptions
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //ImmersionBar.with(this).init()
        dialog?.let { d ->
            d.setCanceledOnTouchOutside(isCancelableOutside)
            if (d.window != null && dialogAnimationRes > 0) {
                d.window!!.setWindowAnimations(dialogAnimationRes)
            }
            if (onKeyListener != null) {
                d.setOnKeyListener(onKeyListener)
            }
        }
    }


    override fun onStart() {
        super.onStart()
        val window = dialog!!.window
        if (window != null) {
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
            //设置窗体背景色透明
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            //设置宽高
            val layoutParams = window.attributes
            if (dialogWidth > 0) {
                layoutParams.width = dialogWidth
            } else {
                if (screenWidthAspect > 0) {
                    if (screenWidthAspect == 1f) {
                        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
                    } else {
                        var w=resources.displayMetrics.widthPixels * screenWidthAspect
                        if (w>1080.dp){
                            w=1080.dp
                        }
                        layoutParams.width =w.toInt()
                    }
                } else {
                    layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT
                }
            }

            if (dialogHeight > 0) {
                layoutParams.height = dialogHeight
            } else {
                if (screenHeightAspect > 0) {
                    if (screenHeightAspect == 1f) {
                        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT
                    } else {
                        layoutParams.height =
                            (resources.displayMetrics.heightPixels * screenHeightAspect).toInt()
                    }
                } else {
                    layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
                }
            }
            //透明度
            layoutParams.dimAmount = dimAmount
            //位置
            layoutParams.gravity = gravity
            window.attributes = layoutParams
        }
    }

    open fun hideKeyboard() {

    }
    fun setDismissListener(listener: () -> Unit) {
        onDismissListener = listener
    }

    override fun onDismiss(dialog: DialogInterface) {
        onDismissListener?.invoke()
        super.onDismiss(dialog)
    }
    private var onDismissListener: (() -> Unit)? = null

    //键盘监听
    var onKeyListener: DialogInterface.OnKeyListener? = null

    //默认弹窗位置为中心
    var gravity: Int = Gravity.CENTER

    //默认宽高为包裹内容
    var dialogHeight: Int = WindowManager.LayoutParams.WRAP_CONTENT
    var dialogWidth: Int = WindowManager.LayoutParams.WRAP_CONTENT

    //触摸空白区域关闭
    var isCancelableOutside: Boolean = true

    //宽高比例
    var screenWidthAspect: Float = 0.85f
    var screenHeightAspect: Float = 0f

    //获取弹窗显示动画
    var dialogAnimationRes: Int = 0

    //背景透明度
    var dimAmount = 0.2f
}
